package com.woniuxy.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.woniuxy.utils.Utils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消息确保型 ：可以告诉用户消费者是否接受到消息
 */
public class ConfirmProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection = Utils.getConnection();
        //对接通道
        Channel channel = connection.createChannel();
        //开启confirm模式
        channel.confirmSelect();
        //增加监听器
        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("----------handleAck----------");
                System.out.println("deliveryTag:"+deliveryTag);
                System.out.println("multiple:"+multiple);
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("----------handleNack----------");
                System.out.println("deliveryTag:"+deliveryTag);
                System.out.println("multiple:"+multiple);
            }
        });
        //发布到对应的交换机和routinkey  prop 表示是否配置消息持久化
        channel.basicPublish("comfirmExchange","confirm1", MessageProperties.PERSISTENT_TEXT_PLAIN,"消息确保型：确保消息发送开始".getBytes());
        int a=10/0;
        channel.basicPublish("comfirmExchange","confirm1",null,"消息确保型：确保消息完成".getBytes());
        System.out.println("发送消息完成");
        //关闭资源
        channel.close();
        connection.close();
    }
}
